package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.dto.UserLoginParamDto;
import com.orientation.entity.User;
import com.orientation.service.UserService;
import com.orientation.vo.LoginVo;
import com.orientation.commom.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "用户管理")
@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public ResponseEntity<Result> insertStudent(@RequestBody User user) {
        userService.insertUser(user);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody User user) {
        return userService.updateById(user);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return userService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public User findById(@PathVariable String id) {
        return userService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<User> findAll() {
        return userService.list();
    }


    @ApiOperation("按照不同条件分页查询")
    @GetMapping("/page")
    public ResponseEntity<Result> getUser(@RequestParam(required = false, defaultValue = "") String name,
                                          @RequestParam(required = false, defaultValue = "") String loginName,
                                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        IPage<User> userIPage = userService.getUser(pageNum, pageSize, name, loginName);
        return ResponseEntity.ok(new Result(userIPage));

    }

    @ApiOperation("分页查询")
    @GetMapping("/page/findPage")
    public IPage<User> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                @RequestParam(required = false, defaultValue = "") String roleId,
                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return userService.page(new Page<>(pageNum, pageSize), Wrappers.<User>lambdaQuery().like(StringUtils.isNotBlank(name), User::getUserName, name).eq(StringUtils.isNotBlank(roleId), User::getRoleId, roleId));
    }


    @ApiOperation("登录")
    @PostMapping("/login")
    public LoginVo login(@RequestBody UserLoginParamDto loginParamDto) {
        return userService.login(loginParamDto);
    }

    @ApiOperation("退出")
    @GetMapping("/logout")
    public void logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
    }

    @ApiOperation("辅导员查询所有")
    @GetMapping("/instructor")
    public List<User> findAllInstructor() {
        return userService.list(Wrappers.<User>lambdaQuery().eq(User::getRoleId, 2));
    }

    @ApiOperation("个人中心")
    @GetMapping("/queryMy")
    public ResponseEntity<Result> queryMy(@RequestParam String loginName) {
        LoginVo loginVo = userService.queryMy(loginName);
        return ResponseEntity.ok(Result.success(loginVo));

    }

    @ApiOperation("个人中心密码修改")
    @PostMapping("/updateMy")
    public ResponseEntity<Result> updateMy(String loginName, String old, String password, String news) {
        userService.updateMy(loginName, old, password, news, null);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("辅导员查询")
    @GetMapping("/queryUser")
    public ResponseEntity<Result> queryUser() {
        List<User> users = userService.queryUser();
        return ResponseEntity.ok(new Result(users));
    }
}
